<!--
 * @Description: 服务管理
 * @Author: DXV-RGWU-TUFH-RFCY-IEGMYY
 * @Date: 2021-07-03 11:41:05
 * @LastEditTime: 2023-08-30 12:37:59
 * @LastEditors: DXV-RGWU-TUFH-RFCY-IEGMYY
-->
<template>
  <div class="lb-store-service">
    <top-nav />
    <div class="page-main">
      <el-row class="page-top-operate">
        <lb-button
          type="primary"
          icon="el-icon-plus"
          @click="toAddService('service')"
          v-hasPermi="`ServiceManage-add`"
          >{{ $t('menu.ServiceAdd') }}</lb-button
        >
        <lb-button
          type="success"
          icon="el-icon-plus"
          @click="toAddService('bell')"
          v-hasPermi="`ServiceBell-add`"
          >{{ $t('menu.ServiceBellAdd') }}</lb-button
        >
      </el-row>
      <el-row class="page-top-operate">
        <lb-button
          type="primary"
          icon="el-icon-plus"
          @click="$router.push(`/store/service/edit`)"
          v-hasPermi="`${$route.name}-add`"
          >{{ $t('menu.ServiceAdd') }}</lb-button
        >
      </el-row>
      <el-row class="page-search-form">
        <el-form
          @submit.native.prevent
          :inline="true"
          :model="searchForm"
          ref="searchForm"
        >
          <el-form-item label="输入查询" prop="name">
            <el-input
              v-model="searchForm.name"
              placeholder="请输入服务名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="服务类型" prop="is_add">
            <el-select
              @change="getTableDataList(1)"
              v-model="searchForm.is_add"
              placeholder="请选择"
            >
              <el-option
                v-for="item in serviceTypeList"
                :key="item.id"
                :label="item.title"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="审核状态" prop="check_status">
            <el-select
              @change="getTableDataList(1)"
              v-model="searchForm.check_status"
              placeholder="请选择"
            >
              <el-option
                v-for="item in userTypeList"
                :key="item.id"
                :label="item.title"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <lb-button
              size="medium"
              type="primary"
              icon="el-icon-search"
              style="margin-right: 5px"
              @click="getTableDataList(1)"
              >{{ $t('action.search') }}</lb-button
            >
            <lb-button
              size="medium"
              icon="el-icon-refresh-left"
              style="margin-right: 5px"
              @click="resetForm('searchForm')"
              >{{ $t('action.reset') }}</lb-button
            >
          </el-form-item>
        </el-form>
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" fixed></el-table-column>
        <el-table-column prop="cover" label="封面图">
          <template slot-scope="scope">
            <lb-image :src="scope.row.cover" />
          </template>
        </el-table-column>
        <el-table-column
          prop="title"
          label="服务名称"
          min-width="120"
        ></el-table-column>
        <el-table-column prop="price" label="服务价格">
          <template slot-scope="scope">
            {{ `¥${scope.row.price}` }}
          </template>
        </el-table-column>
        <!-- <el-table-column prop="init_price" label="服务原价">
          <template slot-scope="scope">
            {{ `¥${scope.row.init_price}` }}
          </template>
        </el-table-column> -->
        <el-table-column prop="time_long" label="服务时长">
          <template slot-scope="scope">
            {{ `${scope.row.time_long}分钟` }}
          </template>
        </el-table-column>
        <el-table-column prop="top" label="排序值"></el-table-column>
        <el-table-column prop="status" label="是否上架">
          <template slot-scope="scope">
            <el-switch
              :disabled="
                $route.meta.pagePermission[0].auth.includes('edit')
                  ? false
                  : true
              "
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              @change="updateItem(scope.row.id, scope.row.status)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间" min-width="120">
          <template slot-scope="scope">
            <p>{{ scope.row.create_time | handleTime(1) }}</p>
            <p>{{ scope.row.create_time | handleTime(2) }}</p>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" min-width="120">
          <template slot-scope="scope" v-if="scope.row.check_status">
            <el-tag :type="statusText[scope.row.check_status].type">
              {{ statusText[scope.row.check_status].text }}
            </el-tag>
            <div class="mt-sm c-link" v-if="scope.row.check_status === 3">
              <el-popover placement="top-start" width="400" trigger="hover">
                <div class="f-desc c-title" slot>
                  <div class="mb-sm f-desc c-title text-bold">
                    {{ scope.row.check_time | handleTime }}
                  </div>
                  <div
                    class="f-caption c-caption"
                    v-html="scope.row.check_text"
                  ></div>
                </div>
                <div slot="reference">查看审核结果</div>
              </el-popover>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="160">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                size="mini"
                plain
                type="primary"
                @click="
                  toAddService(
                    scope.row.is_add ? 'bell' : 'service',
                    scope.row.id
                  )
                "
                v-show="scope.row.check_status !== 1"
                v-hasPermi="`${$route.name}-edit`"
                >{{ $t('action.edit') }}</lb-button
              >
              <lb-button
                size="mini"
                plain
                type="danger"
                @click="confirmDel(scope.row.id)"
                v-hasPermi="`${$route.name}-delete`"
                >{{ $t('action.delete') }}</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import moment from 'moment'
export default {
  data () {
    return {
      serviceTypeList: [{ id: 0, title: '预约服务' }, { id: 1, title: '加钟服务' }],
      userTypeList: [{ id: 0, title: '全部' }, { id: 1, title: '审核中' }, { id: 2, title: '已通过' }, { id: 3, title: '已驳回' }],
      statusText: {
        1: {
          type: 'info',
          text: '审核中'
        },
        2: {
          type: '',
          text: '已通过'
        },
        3: {
          type: 'danger',
          text: '已驳回'
        }
      },
      loading: false,
      searchForm: {
        page: 1,
        limit: 10,
        type: 2,
        name: '',
        is_add: 0,
        check_status: 0
      },
      tableData: [],
      total: 0,
      count: {}
    }
  },
  activated () {
    let { agent_service_type: add } = this.routesItem
    this.searchForm.is_add = add
    this.getTableDataList(1)
  },
  destroyed () {
    this.changeRoutesItem({
      key: 'agent_service_type',
      val: 0
    })
  },
  computed: {
    ...mapState({
      routesItem: state => state.routes
    })
  },
  methods: {
    ...mapMutations(['changeRoutesItem']),
    resetForm (form) {
      this.$refs[form].resetFields()
      this.getTableDataList(1)
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    async toAddService (page, id) {
      this.changeRoutesItem({
        key: 'agent_service_type',
        val: page === 'bell' ? 1 : 0
      })
      let text = id ? `?id=${id}` : ``
      this.$router.push(`/service/${page}/edit${text}`)
    },
    async getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      this.tableData = []
      let { code, data } = await this.$api.service.serviceList(this.searchForm)
      this.loading = false
      if (code !== 200) return
      data.data.map(item => {
        let text = item.check_text
        item.check_text = text && text.length > 0 ? text.replace(/\n/g, '<br>') : '没有填写原因哦'
      })
      this.tableData = data.data
      this.total = data.total
    },
    confirmDel (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      })
        .then(() => {
          this.updateItem(id, -1)
        })
        .catch(() => { })
    },
    async updateItem (id, status) {
      this.$api.service.serviceUpdate({ id, status }).then((res) => {
        if (res.code === 200) {
          this.$message.success(this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper'))
          if (status === -1) {
            this.searchForm.page = this.searchForm.page < Math.ceil((this.total - 1) / this.searchForm.limit) ? this.searchForm.page : Math.ceil((this.total - 1) / this.searchForm.limit)
            this.getTableDataList()
          }
        } else {
          if (status === -1) return
          this.getTableDataList()
        }
      })
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.el-table {
  .table-goods-info {
    width: 280px;
    height: 80px;
    display: flex;
    align-items: center;
    margin-bottom: 10px;
    font-size: 12px;

    .goods-info-r {
      width: 100%;
      display: flex;
      flex-direction: column;
      justify-content: space-around;

      p {
        width: 210px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }

      .price {
        color: red;
      }
    }

    .el-image {
      min-width: 70px;
      height: 70px;
      margin-right: 10px;
    }

    &:last-child {
      margin-bottom: 0;
    }
  }
}
</style>
